package com.sys.readdata;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import com.alibaba.fastjson2.JSON;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/*这里是读入excel需要用的jar包
 * 使用的poi的相关jar包一定版本一定要相同！！！！！也就是这三个包的版本必须一样
 * 坐标：
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>4.0.0</version>
</dependency>
*/
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.Date;


public class Parser {
    public static Data parseXML(String newFileName){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            File f = new File(newFileName);
            Document doc = builder.parse(f);

            Element root = doc.getDocumentElement();
            NodeList trialList = root.getChildNodes();

            Data data = new Data();
            for(int i = 0; i < trialList.getLength(); i++) {
                Node trial = trialList.item(i);
                if(trial instanceof Element) {
                    Leafs leafs = new Leafs();
                    NodeList leftList = trial.getChildNodes();
                    for(int j = 0; j < leftList.getLength(); j++) {
                        Node left = leftList.item(j);
                        if(left instanceof Element) {
                            NodeList leftValueList = left.getChildNodes();
                            String name,value;
                            name = leftValueList.item(1).getTextContent();
                            value = leftValueList.item(3).getTextContent();
                            leafs.add(new Leaf(name,value));
                        }
                    }
                    data.add(leafs);
                }
            }
            return data;
        } catch(Exception e) {
            System.out.println("xml文件格式错误");
        }
        return null;
    }


    public static Data parseExcel(String newFileName) {
        try {

            //data/ExcelData.xlsx

            FileInputStream inputStream = new FileInputStream(newFileName );
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int lastCellNum = 0;
            Data data = new Data();

            String[] names = null;

            for(int rowNum = 0; rowNum < sheet.getPhysicalNumberOfRows(); rowNum++) {
                if(rowNum == 0) {
                    XSSFRow row = (XSSFRow) sheet.getRow(rowNum);
                    lastCellNum = row.getLastCellNum();
                    names = new String[lastCellNum];
                    for(int i = 0; i < lastCellNum; i++) {
                        names[i] = row.getCell(i).getStringCellValue();
                    }
                }
                else {
                    Leafs leafs = new Leafs();
                    XSSFRow row = (XSSFRow) sheet.getRow(rowNum);
                    for(int i = 1; i < lastCellNum; i++) {
                        leafs.add(new Leaf(names[i], row.getCell(i).getRawValue()));
                    }
                    data.add(leafs);
                }
            }
            inputStream.close();
            return data;
        } catch(Exception e) {
            System.out.println("Excel格式不正确");
        }
        return null;
    }

    public static Data parseJson(String newFileName) throws IOException{
        File file = new File(newFileName);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String json = "";
        String line = null;
        while ((line=br.readLine())!=null){
            json+=line;
        }
        Data data = JSON.parseObject(json, Data.class);
        System.out.println(json);
        return data;
    }
}

